<template>
  <el-dialog title="新增部门" :visible="showDialog" @close="handleClose" @open="handleOpen">
    <!-- 匿名插槽 -->
    <el-form ref="fromRef" label-width="120px" :model="from" :rules="fromRules">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="from.name" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="from.code" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="from.manager" style="width:100%">
          <el-option v-for="item in employeeList" :key="item.id" :value="item.username" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="from.introduce" />
      </el-form-item>

    </el-form>
    <!-- el-dialog有专门放置底部操作栏的 插槽  具名插槽 -->
    <div slot="footer">
      <el-button type="primary" size="small" @click="clickSubmit">确定</el-button>
      <el-button size="small" @click="handleClose">取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { reqGetEmployeeSimple } from '@/api/employees'
import { reqAddDepartments } from '@/api/departments'
export default {
  name: 'AddDept',
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    nodeData: {
      type: Object,
      required: true
    },
    originData: {
      type: Array,
      required: true
    }
  },
  data() {
    const checkNameRepeat = (rule, value, callback) => {
      // 筛选财务部部门
      const newArr = this.originData.filter(item => item.pid === this.nodeData.id)
      // 判断添加的新子部门是否在原子部门的数组中
      const repeat = newArr.some(item => item.name === value)
      repeat ? callback(new Error('新增子部门重复')) : callback()
    }
    // 部门编码在整个模块中不能重复
    const checkCodeRepeat = (rule, value, callback) => {
      const repeat = this.originData.some(item => item.code === value)
      repeat ? callback(new Error('新增子部门编码重复')) : callback()
    }
    return {
      from: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      fromRules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 50, message: '部门名称要求1-50个字符', trigger: ['blur', 'change'] },
          { validator: checkNameRepeat, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 50, message: '部门编码要求1-50个字符', trigger: ['blur', 'change'] },
          { validator: checkCodeRepeat, trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '部门负责人不能为空', trigger: ['blur', 'change'] }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 300, message: '部门介绍要求1-300个字符', trigger: ['blur', 'change'] }
        ]
      },
      employeeList: []
    }
  },
  methods: {
    handleClose() {
      // this.$emit('close-dialog')
      this.$emit('update:showDialog', false)
      this.$refs.fromRef.resetFields()
    },
    async handleOpen() {
      const res = await reqGetEmployeeSimple()
      // console.log(res)
      this.employeeList = res.data
    },
    clickSubmit() {
      this.$refs.fromRef.validate(async valid => {
        if (!valid) return
        await reqAddDepartments({ ...this.from, pid: this.nodeData.id })
        // 成功提示
        this.$message.success('新增部门成功')
        // 关闭弹层
        this.handleClose()
        // 重新刷新列表
        this.$emit('add-success')
      })
    }
  }
}
</script>

<style>

</style>
